import util from './util'
import { http, api } from './http'

// const RegionAllItem = { id: 0, name: '全部地区', short_name: '全部地区', region_code: 0 };
// const StoreAllItem = { id: 0, name: '总部' };

let dataCenter = {
  stores: null,
  storeHasNew: true,
  storeIsLoading: false,

  provinces: null,
  cities: null,
  districts: null,
  permissions: null,
  openapiTypes: null,
  deviceModel: {}
}

// dataCenter.getDeviceModel = deviceType => {
//     return http.req(api.ticket_device_model, null, { service_target: deviceType }).then(res => {
//         dataCenter.deviceModel[deviceType] = res;
//         return res;
//     });
// };

dataCenter.getDeviceModel = (vm, deviceType, clientId, service_type) => {
  const data = { service_target: deviceType, client_id: clientId || null }
  if (service_type) {
    data.service_type = 4
  }
  return http.fetch(vm, api.ticket_device_model, null, data).then(res => {
    return res
  })
}

/**
 * 初始化门店列表
 * @param {Function} cb 回调函数
 */
dataCenter.initStores = function (vm, cb) {
  // if (dataCenter.storeIsLoading) {
  //     return;
  // }

  dataCenter.storeIsLoading = true
  http.request(vm, api.branch_all, null, null, (res, err) => {
    if (err || !res) {
      return
    }
    // res.unshift(StoreAllItem)
    dataCenter.storeHasNew = false
    dataCenter.storeIsLoading = false
    dataCenter.stores = res
    typeof cb === 'function' && cb(util.deepNormalCopy(res))
  })
}

/**
 * 获取门店列表
 * @param {Function} cb 回调函数
 */
dataCenter.getStores = function (vm, cb) {
  if (dataCenter.storeHasNew) {
    dataCenter.initStores(vm, cb)
    return
  }

  if (dataCenter.stores) {
    typeof cb === 'function' && cb(util.deepNormalCopy(dataCenter.stores))
  } else {
    dataCenter.initStores(vm, cb)
  }
}

/**
 * 更新门店列表
 * @param {Object} params 参数
 */
dataCenter.updateStores = function (vm) {
  dataCenter.storeHasNew = true
  dataCenter.initStores(vm)
}

/**
 * 获取省份列表
 * @param {Function} cb 回调函数
 */
dataCenter.getProvinces = function (vm, cb) {
  if (dataCenter.provinces) {
    typeof cb === 'function' && cb(util.deepNormalCopy(dataCenter.provinces))
    return
  }

  http.request(vm, api.address_provinces, null, null, (res, err) => {
    if (err || !res) {
      return
    }

    // res.unshift(RegionAllItem);
    dataCenter.provinces = res
    typeof cb === 'function' && cb(util.deepNormalCopy(dataCenter.provinces))
  })
}

/**
 * 获取所有权限列表
 * @param {Function} cb 回调函数
 */
dataCenter.getPermissions = function (vm, cb) {
  if (dataCenter.permissions) {
    typeof cb === 'function' && cb(util.deepNormalCopy(dataCenter.permissions))
    return
  }

  http.request(vm, api.permission_list, null, null, (res, err) => {
    if (err || !res) {
      return
    }
    dataCenter.permissions = res
    typeof cb === 'function' && cb(util.deepNormalCopy(dataCenter.permissions))
  })
}

/**
 * 获取城市列表
 * @param {Number} provinceId 省份id
 * @param {Function} cb 回调函数
 */
dataCenter.getCities = function (vm, provinceId, cb) {
  let cities = dataCenter.cities || {}
  let key = provinceId || 'all'

  if (cities[key]) {
    typeof cb === 'function' && cb(util.deepNormalCopy(cities[key]))
    return
  }

  http.request(vm, api.address_cities, null, { province_id: provinceId }, (res, err) => {
    if (err || !res) {
      return
    }

    // res.unshift(RegionAllItem);
    cities[key] = res
    dataCenter.cities = cities
    typeof cb === 'function' && cb(util.deepNormalCopy(cities[key]))
  })
}

/**
 * 获取区列表
 * @param {Number} cityId 城市id
 * @param {Function} cb 回调函数
 */
dataCenter.getDistricts = function (vm, cityId, cb) {
  let districts = dataCenter.districts || {}

  if (districts[cityId]) {
    typeof cb === 'function' && cb(util.deepNormalCopy(districts[cityId]))
    return
  }

  http.request(vm, api.address_districts, null, { city_id: cityId }, (res, err) => {
    if (err || !res) {
      return
    }

    // res.unshift(RegionAllItem);
    districts[cityId] = res
    dataCenter.districts = districts
    typeof cb === 'function' && cb(util.deepNormalCopy(districts[cityId]))
  })
}

dataCenter.getOpenapiType = async function () {
  if (dataCenter.openapiTypes) return dataCenter.openapiTypes
  try {
    const res = await http.req(api.openapi_operations_types, null, { list_type: 'all', origin: 2 })
    if (!res) return []
    dataCenter.openapiTypes = res.rows
    return dataCenter.openapiTypes
  } catch (err) {
    console.log(err)
  }
}

/**
 * 重置 dataCenter 数据
 */
dataCenter.reset = () => {
  dataCenter.stores = null

  dataCenter.provinces = null
  dataCenter.cities = null
  dataCenter.districts = null
  dataCenter.permissions = null

  dataCenter.storeHasNew = true
  dataCenter.storeIsLoading = false
}

export default dataCenter
